/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PanelBuscarHistoriaClinica.java
 *
 * Created on 29/03/2012, 05:18:49 PM
 */
package co.edu.eam.clinica.vista.gui.historiaclinica;

import co.edu.eam.clinica.vista.controladores.historiaclinica.controladorHistoriaClinica;
import co.edu.eam.clinica.vista.gui.common.PanelOncologos;
import co.edu.eam.clinica.vista.gui.common.VentanaPrincipalManager;
import co.edu.eam.persitencia.entidades.HistoriaClinica;
import co.edu.eam.persitencia.entidades.Paciente;
import co.edu.eam.persitencia.enumeraciones.TipoDocumentoEnum;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Camilo Andres
 */
public class PanelBuscarHistoriaClinica extends PanelOncologos {

    private controladorHistoriaClinica controladorH;

    /**
     * Creates new form PanelBuscarHistoriaClinica
     */
    public PanelBuscarHistoriaClinica() {
        initComponents();
        setVisible(true);
        controladorH = new controladorHistoriaClinica();
        cargarEnum();
    }
    
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel2 = new javax.swing.JPanel();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        tfDoc = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        cbTipoDoc = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        tfNom = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        tfEPS = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        tfEdad = new javax.swing.JTextField();
        bBuscar = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tablaHistoria = new javax.swing.JTable();
        jButton1 = new javax.swing.JButton();
        bTodos = new javax.swing.JButton();

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Paciente", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 12))); // NOI18N

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel1.setText("Documento");

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel2.setText("Tipo Documento");

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel3.setText("Nombre");

        tfNom.setEditable(false);

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel4.setText("EPS");

        tfEPS.setEditable(false);

        jLabel5.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel5.setText("Edad");

        tfEdad.setEditable(false);

        bBuscar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/torresquintero/oncologos/medico/imagenes/imagenes/24x24/buscar-buscar-ampliar-icono-4271-32.png"))); // NOI18N
        bBuscar.setText("Buscar");
        bBuscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bBuscarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel3)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(bBuscar)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addComponent(tfDoc, javax.swing.GroupLayout.PREFERRED_SIZE, 192, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(jLabel2)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(cbTipoDoc, javax.swing.GroupLayout.PREFERRED_SIZE, 216, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addComponent(tfNom)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addComponent(tfEPS, javax.swing.GroupLayout.PREFERRED_SIZE, 286, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(jLabel5)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(tfEdad))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(tfDoc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2)
                    .addComponent(cbTipoDoc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(tfNom, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(tfEPS, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5)
                    .addComponent(tfEdad, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(bBuscar)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        tablaHistoria.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null}
            },
            new String [] {
                "Documento Paciente", "Nombre", "EPS"
            }
        ));
        jScrollPane1.setViewportView(tablaHistoria);

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/torresquintero/oncologos/medico/imagenes/imagenes/24x24/historiaclinica.jpg"))); // NOI18N
        jButton1.setText("Ver Historia");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        bTodos.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/torresquintero/oncologos/medico/imagenes/imagenes/24x24/tipo-gtk-icono-8624-32.png"))); // NOI18N
        bTodos.setText("Todos");
        bTodos.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bTodosActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 115, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(bTodos)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 160, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(bTodos))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void bTodosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bTodosActionPerformed
        try {
            // TODO add your handling code here:

           DefaultTableModel modelo = (DefaultTableModel) tablaHistoria.getModel();

           List<HistoriaClinica> lista = controladorH.listarHistoriasClinicas();
            modelo.setRowCount(lista.size());
                

            for (int i = 0; i < lista.size(); i++) {

                HistoriaClinica h = (HistoriaClinica) lista.get(i);

                modelo.setValueAt(h.getPaciente().getDocumento(), i, 0);
                modelo.setValueAt(h.getPaciente(), i, 1);
                modelo.setValueAt(h.getPaciente().getEps(), i, 2);

            }
           
        } catch (Exception ex) {
            Logger.getLogger(PanelBuscarHistoriaClinica.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_bTodosActionPerformed

    private void cargarEnum(){
    TipoDocumentoEnum[] documentoEnum = TipoDocumentoEnum.values();
        for (int i = 0; i < documentoEnum.length; i++) {
           cbTipoDoc.addItem(documentoEnum[i]);
        }
    }
    
    private void bBuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bBuscarActionPerformed
        try {
            Paciente paciente = controladorH.buscarPaciente(tfDoc.getText());

            tfEPS.setText(paciente.getEps().getNombre());
            tfNom.setText(paciente.getNombre());
            cbTipoDoc.addItem(paciente.getTipoDocumento());
            tfEdad.setText(new Date().getYear()-paciente.getFechaNacimiento().getYear() + "");


           DefaultTableModel modelo = (DefaultTableModel) tablaHistoria.getModel();

            HistoriaClinica historia = controladorH.buscarHistoriaClinica(tfDoc.getText());

            modelo.setValueAt(historia.getPaciente().getDocumento(), 0, 0);
            modelo.setValueAt(historia.getPaciente(), 0, 1);
            modelo.setValueAt(historia.getPaciente().getEps(), 0, 2);

            
        } catch (Exception ex) {
            Logger.getLogger(PanelBuscarHistoriaClinica.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_bBuscarActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed


        DefaultTableModel modelo = (DefaultTableModel) tablaHistoria.getModel();
        int id = tablaHistoria.getSelectedRow();
        String documento =  (String) tablaHistoria.getValueAt(id, 0);

        PanelHIstoriaNueva panel = new PanelHIstoriaNueva(documento);
        VentanaPrincipalManager.agregarVentana(panel);
        
        this.tablaHistoria.setModel(modelo);
        
    }//GEN-LAST:event_jButton1ActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bBuscar;
    private javax.swing.JButton bTodos;
    private javax.swing.JComboBox cbTipoDoc;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tablaHistoria;
    private javax.swing.JTextField tfDoc;
    private javax.swing.JTextField tfEPS;
    private javax.swing.JTextField tfEdad;
    private javax.swing.JTextField tfNom;
    // End of variables declaration//GEN-END:variables
}
